package com.example.boot.service

import com.example.boot.domain.User
import com.example.boot.dto.UserDTO
import com.example.boot.mapper.UserMapper
import com.example.boot.repository.UserRepository
import com.ninjasquad.springmockk.MockkBean
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import io.mockk.*
import org.springframework.beans.factory.annotation.Autowired

@SpringBootTest
class UserServiceImplTest {

    @MockkBean
    lateinit var userRepository: UserRepository

    @Autowired
    lateinit var userMapper: UserMapper

    @Autowired
    lateinit var userService: UserService

    @Test
    fun `wrong test`() {
        val user = UserDTO(1L, "test", 29)
        every {
            userRepository.existsByName(user.name)
        } returns false
        val created = userService.createUser(user)
        println(created.name)
    }

    @Test
    fun `wrong test 2`() {
        val user = UserDTO(1L, "test", 29)
        val created = userService.createUser(user)
        println(created.name)
    }

    @Test
    fun `correct test`() {
        val user = UserDTO(1L, "test", 29)
        val entity = userMapper.convertToDomain(user)

        every {
            userRepository.existsByName(user.name)
        } returns false
        every {
            userRepository.save(any())
        } returns entity

        val created = userService.createUser(user)
        println(created.name)

        verify {
            userRepository.existsByName(user.name)
        }

//        verify {
//            userRepository.save(allAny())
//        }

//        verify {
//            userRepository.save(match {
//                it.name.equals(user.name) && it.id.equals(user.id) && it.age == user.age
//            })
//        }

        verify {
            userRepository.save(match {
                it.name.equals(user.name) && it.id.equals(user.id) && it.age == user.age
            })
        }

//        verify {
//            userRepository.save(range(from = entity, to = entity))
//        }
    }

    @Test
    fun demo() {
        val m = mutableMapOf<String, String>()

        val aa = null
        val bb = aa as String
        print(bb)
        val a = true
        m["a"] = if(a) {
            "a"
        } else {
            "b"
        }
    }


}